package com.controller;

import com.entity.Person;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * EnableAutoConfiguration注解少了springBoot启动后将会扫描不到controller
 * <p>
 * Created by Administrator on 2018-9-26 0026.
 */
@Controller
@EnableAutoConfiguration
@RequestMapping("/home")
@Api("BaseController相关api")
public class BaseController {

    private static final Logger logger = LogManager.getLogger(BaseController.class);

    @Autowired
    private PersonService personService;

    @RequestMapping("/index")
    public String index() throws Exception {
        List<Person> list = personService.getAll();
        System.out.println("是否扫描到controller跟dao:  " + list.size());
        return "forward:/1.html";
    }

    @ResponseBody
    @RequestMapping("/test")
    public Map<String, String> test() {
        Map<String, String> map = Maps.newHashMap();
        String code = "1";
        String msg = "success";
        map.put(code, msg);
        logger.info("map{}", map);
        return map;
    }

    @ResponseBody
    @RequestMapping("/pageList")
    @ApiOperation(value = "查询分页信息", notes = "只需要当前页与页大小")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前多少页", paramType = "query",  dataType = "int", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "一页显示多少条", paramType = "query", dataType = "int", example = "2", required = true)
    })
    public List<Person> getListForPage(@RequestParam int pageIndex, @RequestParam int pageSize) throws Exception {
        PageHelper.startPage(pageIndex, pageSize);
        List<Person> personList = personService.getPageList(pageIndex, pageSize);
        return personList;
    }

    @ResponseBody
    @RequestMapping("/pageInfo")
    public PageInfo getPageInfo(@RequestParam int pageIndex, @RequestParam int pageSize) throws Exception {
        PageHelper.startPage(pageIndex, pageSize);
        List<Person> personList = personService.getPageList(pageIndex, pageSize);
        PageInfo pageInfo = PageInfo.of(personList);
        return pageInfo;
    }

}
